Completed
Pull Request — master (#67)
by Michael
02:02
created

hAtPosCommand   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 3
dl 0
loc 18
rs 9.2833
c 0
b 0
f 0
nop 2
1
import { Selection } from 'prosemirror-state';
2
3
/**
4
 * Returns a command that tries to set the selected textblocks to the given node type with the given attributes.
5
 *
6
 * Copied and adjusted from prosemirror-commands::setBlockType to not check for the node attributes
7
 */
8
export function setBlockTypeNoAttrCheck(nodeType, attrs) { // eslint-disable-line import/prefer-default-export
9
    return function setBlockTypeNoAttrCheckDispatch(state, dispatch) {
10
        const { from, to } = state.selection;
11
        let applicable = false;
12
        state.doc.nodesBetween(from, to, (node, pos) => {
13
            if (applicable) return false;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
14
            if (!node.isTextblock || node.type === nodeType) return true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
15
            const $pos = state.doc.resolve(pos);
16
            const index = $pos.index();
17
            applicable = $pos.parent.canReplaceWith(index, index + 1, nodeType);
18
            return true;
19
        });
20
        if (!applicable) return false;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
21
        if (dispatch) dispatch(state.tr.setBlockType(from, to, nodeType, attrs).scrollIntoView());
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
22
        return true;
23
    };
24
}
25
26
/**
27
 * Returns a command that inserts a new paragraph before or after the node at the given position
28
 *
29
 * @param {int}    pos       position near which the paragraph should be inserted
30
 * @param {string} direction should be 'before' or 'after'
31
 * @return {function}
32
 */
33
export function insertParagraphAtPos(pos, direction = 'after') {
34
    return function insertParagraphAtPosCommand(state, dispatch) {
35
        const $pos = state.doc.resolve(pos);
36
        const above = $pos.node(-1);
37
        const beforeOrAfter = direction !== 'after' ? $pos.index(-1) : $pos.indexAfter(-1);
38
        const type = above.contentMatchAt(beforeOrAfter).defaultType;
39
40
        if (!above.canReplaceWith(beforeOrAfter, beforeOrAfter, type)) {
41
            return false;
42
        }
43
44
        if (dispatch) {
45
            const insertPos = direction !== 'after' ? $pos.before() : $pos.after();
46
            const tr = state.tr.replaceWith(insertPos, insertPos, type.createAndFill());
47
            tr.setSelection(Selection.near(tr.doc.resolve(insertPos), 1));
48
            dispatch(tr.scrollIntoView());
49
        }
50
        return true;
51
    };
52
}
53